package com.logos.ruralrevitalization.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.logos.ruralrevitalization.req.TouristAttractionsReq;
import com.logos.ruralrevitalization.resp.TouristAttractionsResp;
import com.logos.ruralrevitalization.result.Result;
import com.logos.ruralrevitalization.service.TouristAttractionsService;
import com.logos.ruralrevitalization.utils.RuralLoginUtil;
import com.logos.ruralrevitalization.utils.TouristLoginUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 旅游景点表(TouristAttractions)表控制层
 *
 * @author rose
 * @since 2025-03-25 22:09:06
 */
@RestController
@RequestMapping("/touristAttractions")
@Slf4j
public class TouristAttractionsController {

    @Resource
    private TouristAttractionsService touristAttractionsService;

    @Resource
    private RuralLoginUtil ruralLoginUtil;

    /**
     * 查询景点（可根据类型）
     */
    @PostMapping("/query")
    public Result<List<TouristAttractionsResp>> queryByType(@RequestBody TouristAttractionsReq touristAttractionsReq) {
        try {
            if (log.isInfoEnabled()) {
                log.info("TouristAttractionsController.queryByType.touristAttractionsReq:{}", JSON.toJSONString(touristAttractionsReq));
            }
            Preconditions.checkNotNull(touristAttractionsReq.getPageNo(), "分页码不能为空");
            Preconditions.checkNotNull(touristAttractionsReq.getPageSize(), "分页大小不能为空");
            List<TouristAttractionsResp> result = touristAttractionsService.queryByType(touristAttractionsReq);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("TouristAttractionsController.queryByType.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 热门景点排行榜
     */
    @GetMapping("/hotRank")
    public Result<List<TouristAttractionsResp>> queryHotRank() {
        try {
            List<TouristAttractionsResp> result = touristAttractionsService.queryHotRank();
            return Result.ok(result);
        } catch (Exception e) {
            log.error("TouristAttractionsController.queryHotRank.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 查看某个旅游景点详情
     */
    @GetMapping("/{id}")
    public Result<TouristAttractionsResp> queryDetail(@PathVariable("id") Integer id) {
        try {
            log.info("TouristAttractionsController.queryById.id:{}", id);
            TouristAttractionsResp result = touristAttractionsService.queryDetail(id);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("TouristAttractionsController.queryById:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }


    /**
     * 查询农户上架的旅游景点
     */
    @GetMapping("/queryRuralTourist")
    public Result<List<TouristAttractionsResp>> queryRuralTourist() {
        try {
            String id = ruralLoginUtil.getId();
            if (StringUtils.isBlank(id)) {
                return Result.fail("请先登录");
            }
            log.info("AgriculturalProductController.queryRuralProd.id:{}", id);
            List<TouristAttractionsResp> result = touristAttractionsService.queryRuralTourist(Long.valueOf(id));
            return Result.ok(result);
        } catch (Exception e) {
            log.error("RuralController.updateInfo.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }


    /**
     * 上架景点
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody TouristAttractionsReq touristAttractionsReq) {
        try {
            if (log.isInfoEnabled()) {
                log.info("TouristAttractionsController.add.touristAttractionsReq:{}", JSON.toJSONString(touristAttractionsReq));
            }
            Preconditions.checkNotNull(touristAttractionsReq.getName(), "景点名称不能为空");
            Preconditions.checkNotNull(touristAttractionsReq.getDescription(), "景点描述不能为空");
            Preconditions.checkNotNull(touristAttractionsReq.getPrice(), "景点价格不能为空");
            Preconditions.checkNotNull(touristAttractionsReq.getType(), "景点类型不能为空");
            Preconditions.checkNotNull(touristAttractionsReq.getAvatar(), "景点图片不能为空");
            String id = ruralLoginUtil.getId();
            if (StringUtils.isBlank(id)) {
                return Result.fail("请先登录");
            }
            touristAttractionsReq.setSupplierId(Long.valueOf(id));
            Boolean result = touristAttractionsService.add(touristAttractionsReq);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("TouristAttractionsController.add.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 下架景点
     */
    @PostMapping("/down")
    public Result<Boolean> down(@RequestBody TouristAttractionsReq touristAttractionsReq) {
        try {
            if (log.isInfoEnabled()) {
                log.info("TouristAttractionsController.down.touristAttractionsReq:{}", JSON.toJSONString(touristAttractionsReq));
            }
            Preconditions.checkNotNull(touristAttractionsReq.getId(), "景点id不能为空");
            Boolean result = touristAttractionsService.down(touristAttractionsReq);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("TouristAttractionsController.down.error:{}", e.getMessage(), e);
            return Result.fail("下架景点失败");
        }
    }

    /**
     * 更新景点信息
     */
    @PostMapping("/updateInfo")
    public Result<Boolean> updateInfo(@RequestBody TouristAttractionsReq touristAttractionsReq) {
        try {
            if (log.isInfoEnabled()) {
                log.info("TouristAttractionsController.updateInfo.touristAttractionsReq:{}", JSON.toJSONString(touristAttractionsReq));
            }
            Preconditions.checkNotNull(touristAttractionsReq.getId(), "景点id不能为空");
            Boolean result = touristAttractionsService.updateInfo(touristAttractionsReq);
            return Result.ok(result);
        } catch (Exception e) {
            log.error("TouristAttractionsController.updateInfo.error:{}", e.getMessage(), e);
            return Result.fail("更新景点信息失败");
        }
    }

    /**
     * 轮播图展示热门景点
     */
    @GetMapping("/banner")
    public Result<List<TouristAttractionsResp>> queryBanner() {
        try {
            List<TouristAttractionsResp> result = touristAttractionsService.queryBanner();
            return Result.ok(result);
        } catch (Exception e) {
            log.error("TouristAttractionsController.queryBanner.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 首页展示热门景点
     */
    @GetMapping("/showHome")
    public Result<List<TouristAttractionsResp>> queryHome() {
        try {
            List<TouristAttractionsResp> result = touristAttractionsService.queryHome();
            return Result.ok(result);
        } catch (Exception e) {
            log.error("TouristAttractionsController.queryHome.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

}

